package role

import (
	"context"
	"gorm.io/gorm"
	"trusteva/app/rbac/model"

	"trusteva/app/rbac/internal/svc"
	"trusteva/app/rbac/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RoleDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleDeleteLogic {
	return &RoleDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleDeleteLogic) RoleDelete(req *types.RoleDeleteRo) (resp *types.BaseRsp, err error) {
	err = l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("Id=?", req.RoleId).
			Delete(&model.RoleConfig{}).Error; err != nil {
			return err
		}
		if err = tx.Where("RoleId=?", req.RoleId).
			Delete(&model.RoleMenuAction{}).Error; err != nil {
			return err
		}

		return nil
	})

	return
}
